using System;
using System.Reflection;
using NUnit.Framework;
using ContentRenderingApi;
using ContentRenderingApiSystemTest.Helper;
using ContentRenderingApi.Path;

namespace ContentRenderingApiSystemTest.TestFixture
{
    [TestFixture]
    public class RenderFormatterTestFixture
    {
        [Test]
        public void FormatterTest()
        {
            AbstractTemplate template = new EmbeddedResourceTemplate(
                Assembly.GetExecutingAssembly(),
                "ContentRenderingApiSystemTest.TestFixture.Input",
                "GlobalFormatter");

            template.AddNamedParameter("lowerCase", "Test1");
            template.AddNamedParameter("month", new DateTime(2009, 6, 1));

            template.TransformationConfiguration.AddGlobalFormatter<string>(value => ((string)value).ToUpper());
            template.TransformationConfiguration.AddGlobalFormatter<DateTime>(value => ((DateTime)value).Year.ToString());
            
            Assert.AreEqual(
                ResourceHelper.FetchString("ContentRenderingApiSystemTest.TestFixture.Output.GlobalFormatter.txt"),
                template.Render());
        }

        [Test]
        public void LocalFormatterTest()
        {
            AbstractTemplate template = new EmbeddedResourceTemplate(
                Assembly.GetExecutingAssembly(),
                "ContentRenderingApiSystemTest.TestFixture.Input",
                "LocalFormatter");

            template.AddNamedParameter("lowerCase", "Test1");
            template.AddNamedParameter("month", new DateTime(2009, 6, 1));
            template.AddNamedParameter("testa", new { testb = new DateTime(2009, 6, 1) });
            template.AddNamedParameter("testc", new DateTime(2009, 6, 1));

            template.TransformationConfiguration.AddLocalFormatter<string>(new PartialPath("lowerCase"), value => ((string)value).ToLower());
            template.TransformationConfiguration.AddLocalFormatter<DateTime>(new PartialPath("month"), value => ((DateTime)value).Month.ToString());
            template.TransformationConfiguration.AddLocalFormatter<DateTime>(new FullPath("testa", "testb"), value => ((DateTime)value).Year.ToString());
            template.TransformationConfiguration.AddLocalFormatter<DateTime>(new FullPath("testc"), value => ((DateTime)value).Day.ToString());

            Assert.AreEqual(
                ResourceHelper.FetchString("ContentRenderingApiSystemTest.TestFixture.Output.LocalFormatter.txt"),
                template.Render());
        }

        [Test]
        public void WrongParameterNamedFormatterTest()
        {
            AbstractTemplate template = new EmbeddedResourceTemplate(
                Assembly.GetExecutingAssembly(),
                "ContentRenderingApiSystemTest.TestFixture.Input",
                "WrongParameterLocalFormatter");

            template.AddNamedParameter("lowerCase1", "Test1");
            template.AddNamedParameter("month1", new DateTime(2009, 6, 1));

            template.TransformationConfiguration.AddLocalFormatter<string>(new PartialPath("lowerCase"), value => ((string)value).ToLower());
            template.TransformationConfiguration.AddLocalFormatter<DateTime>(new PartialPath("month"), value => ((DateTime)value).Month.ToString());

            Assert.AreEqual(
                ResourceHelper.FetchString("ContentRenderingApiSystemTest.TestFixture.Output.WrongParameterLocalFormatter.txt"),
                template.Render());
        }

        [Test]
        public void OverridesGenericNamedFormatterTest()
        {
            AbstractTemplate template = new EmbeddedResourceTemplate(
                Assembly.GetExecutingAssembly(),
                "ContentRenderingApiSystemTest.TestFixture.Input",
                "OverridesGenericLocalFormatter");

            template.AddNamedParameter("lowerCase", "Test1");
            template.AddNamedParameter("month", new DateTime(2009, 6, 1));

            template.TransformationConfiguration.AddLocalFormatter<string>(new PartialPath("lowerCase"), value => ((string)value).ToLower());
            template.TransformationConfiguration.AddLocalFormatter<DateTime>(new PartialPath("month"), value => ((DateTime)value).Month.ToString());
            template.TransformationConfiguration.AddGlobalFormatter<string>(value => ((string)value).ToUpper());
            template.TransformationConfiguration.AddGlobalFormatter<DateTime>(value => ((DateTime)value).Year.ToString());

            Assert.AreEqual(
                ResourceHelper.FetchString("ContentRenderingApiSystemTest.TestFixture.Output.OverridesGenericLocalFormatter.txt"),
                template.Render());
        }
    }
}
